#ifndef __MY_KEY_DRV_H__
#define __MY_KEY_DRV_H__

#include "n32l40x.h"

// POWER_MODE_KEY, LEVEL_ADD_KEY 平时为高电平，按键动作时为低点平

#define POWER_MODE_KEY_PORT      GPIOD 
#define POWER_MODE_KEY_PIN       GPIO_PIN_14  


#define LEVEL_ADD_KEY_PORT      GPIOC
#define LEVEL_ADD_KEY_PIN       GPIO_PIN_14

// // level_dec_key ,平时为低电平，按键动作时为高电平
// #define LEVEL_DEC_KEY_PORT      GPIOA 
// #define LEVEL_DEC_KEY_PIN     	GPIO_PIN_15   


//According to your need to modify the constants.
#define TICKS_INTERVAL      5 //20	//ms
#define DEBOUNCE_TICKS      6	//MAX 8
#define MIN_SHORT_TICKS     (100 /TICKS_INTERVAL)
#define MAX_SHORT_TICKS     (400 /TICKS_INTERVAL)
#define LONG_TICKS          (1900 /TICKS_INTERVAL)	// 开机bootloader占用了100ms时间，所以在这里减少100ms


typedef void (*btn_callback)(void *);

typedef enum {
	PRESS_DOWN = 0,
	PRESS_UP,
	PRESS_REPEAT,
	SINGLE_CLICK,
	DOUBLE_CLICK,
	LONG_PRESS_START,
	LONG_PRESS_HOLD,
	LONG_PRESS_RELEASE,
	number_of_event,
	NONE_PRESS
} PRESS_EVT;

typedef enum {
	LOW_LEVEL_TRIGGER = 0,
	HIGH_LEVEL_TRIGGER,
	ACTIVE_LEVEL_INVALID,
} ACTIVE_LEVELS_EVT;

typedef struct _button_ {
    GPIO_Module*  port;
    uint16_t  pin;
	uint16_t ticks;
	uint8_t  repeat : 4;
	uint8_t  event : 4;
	uint8_t  state : 3;
	uint8_t  debounce_cnt : 3;
	uint8_t  active_level : 1;
	uint8_t  button_level : 1;
	void *user_data;
	uint8_t (*hal_button_Level)(struct _button_ *);
	btn_callback  cb[number_of_event];
	struct _button_ *next;
} BUTTON_S;


void button_init(BUTTON_S *handle, GPIO_Module* port, uint16_t pin, uint8_t active_level);
void button_attach(BUTTON_S *handle, PRESS_EVT event, btn_callback cb);
PRESS_EVT button_get_event(BUTTON_S *handle);



void my_key_init(void);
void my_key_scanf(void);


#endif
